package Display;

//copyright (c) Hiroyuki Murata 1997.
//free to use, distribute or modify.
import java.io.*;

public class Native2Utf {

 static String usage =
     " Native text to UTF-8 Converter for JDK 1.1.x.\n" +
     "  Usage: java Native2Utf [-e encoding] nativefile outputfile\n" +
     "    encoding = \"SJIS\"(default), \"EUCJIS\" or \"JIS\""
 ;

 static final int MAXLEN = 1024;
 static char[] buf = new char[MAXLEN];
 static String
     encoding = "SJIS",
     rfile, wfile
 ;

 static public void main(String[] args) {

     try {
         int argi=0;
         if (args[0].equals("-e")) {
             encoding = args[1];
             argi = 2;
         }
         rfile = args[argi++];
         wfile = args[argi];
     } catch (ArrayIndexOutOfBoundsException e) {
         System.out.print(usage);
         System.exit(0);
     }

     try {
         InputStreamReader reader = new InputStreamReader(
             new BufferedInputStream(
                 new FileInputStream(rfile)
             ),
             encoding
         );
         DataOutputStream dos = new DataOutputStream (
             new BufferedOutputStream(
                 new FileOutputStream(wfile)
             )
         );

         int ci, column = 0;

         for (;;) {
             ci = reader.read();
             char cc = (char)ci;
             if (ci == -1) {
                 dos.close();
                 reader.close();
                 System.exit(0);
             }
             if (column >= MAXLEN) {
                 System.err.println("too long line!");
                 System.exit(1);
             }

             buf[column++] = cc;

             if (cc == '\n') {
                 String line = new String(buf, 0, column);
                 dos.writeUTF(line);
                 //System.out.print(line);
                 column = 0;
             }
         }

     } catch (FileNotFoundException e) {
         System.err.println("file " + rfile + " not found.");
     } catch (UnsupportedEncodingException e) {
         System.err.println("encoding " + encoding + " not supported.");
     } catch (IOException e) {
         e.printStackTrace();
     }
 }
}